package cn.xierongsheng.mailserve.controller;

import cn.xierongsheng.mailserve.common.api.CommonResult;
import cn.xierongsheng.mailserve.dto.SendData;
import cn.xierongsheng.mailserve.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mail")
public class MailController {

    @Autowired
    private EmailService emailService;

    @PostMapping("/sendEmail")
    public CommonResult sendEmail(@RequestBody SendData sendData) {

        try {
            emailService.sendEmailToSelf(sendData.getTo(), sendData.getName(), sendData.getPhone(), sendData.getSubject(), sendData.getContent());
            emailService.sendEmailToHr(sendData.getTo(), sendData.getName());
            return CommonResult.success("邮件发送成功！");
        } catch (Exception e) {
            // 处理异常
            e.printStackTrace();
            return CommonResult.failed("邮件发送失败！");
        }
    }
}
